﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Objects.CMT;

public partial class ViewFeedBack : Objects.cSecurePage
{
    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindEvents();
            dgFeedback.Visible = false;
            dgFeedbackSummary.Visible = false;
        }
        ddlEvent.SelectedIndexChanged += new EventHandler(ddlEvent_select);
    }

    public void ddlEvent_select(object sender, EventArgs e)
    {
        if (ddlEvent.SelectedValue != "0")
        {

            bindFeedback();
            plFeedback.Visible = false;

        }
    }

    protected void btnSubmitt_click(object sender, EventArgs e)
    {
        if (ddlEvent.SelectedValue != "0")
        {

            bindFeedback();
            plFeedback.Visible = false;

        }
    }

    protected void dgFeedback_ItemCommand(object sender, GridViewCommandEventArgs e)
    {
        plFeedback.Visible = false;

        if (e.CommandName.Equals("ShowFeedBak"))
        {
            GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            ViewState["eventAttendeeId"] = rowIndex;



            CMTFeedback objFeedback = new CMTFeedback();
            DataTable dt = objFeedback.GetFeedbackDetails(ddlEvent.SelectedValue.ToString(), rowIndex.ToString(), "0");
            if (dt.Rows.Count > 0)
            {
                dgFeedbackSummary.Visible = true;

                dgFeedbackSummary.DataSource = dt;
                dgFeedbackSummary.DataBind();
            }
            else
            {
                dgFeedbackSummary.Visible = false;
            }
        }
    }

    protected void dgFeedbackSummary_ItemCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("ShowFeedBak"))
        {
            plFeedback.Visible = true;
            GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            int rowIndex = Convert.ToInt32(e.CommandArgument);

            CMTFeedback objFeedback = new CMTFeedback();
            DataTable dt = objFeedback.GetFeedbackDetails(ddlEvent.SelectedValue.ToString(), "0", rowIndex.ToString());
            lblSSL.Text = dt.Rows[0]["SubServiceLine"].ToString();
            lblstage.Text = dt.Rows[0]["StageName"].ToString();
            lblResponsiblity.Text = dt.Rows[0]["ResponsibilityTakeForward"].ToString();
            lblProbWin.Text = dt.Rows[0]["WinName"].ToString();
            lblPotentialTaxFees.Text = dt.Rows[0]["PotentialTaxFees"].ToString();
            lblOpportninty.Text = dt.Rows[0]["OpportunityProjectIdentified"].ToString();
            lblNextSteps.Text = dt.Rows[0]["NextSteps"].ToString();
            lblIssuesAddressed.Text = dt.Rows[0]["PrimTaxIssues"].ToString();
            lblExpectedTimeFrame.Text = dt.Rows[0]["TimeFrame"].ToString();
            lblDescofOpport.Text = dt.Rows[0]["DescrOpportunityProject"].ToString();
            lblCountry.Text = dt.Rows[0]["Country"].ToString();
            lblBusinnessObj.Text = dt.Rows[0]["PrimObjectivesChallenges"].ToString();
            lblAdditionalInfo.Text = dt.Rows[0]["AdditionalInformation"].ToString();
        }
        else if (e.CommandName.Equals("ExportExcel"))
        {
            plFeedback.Visible = true;
            GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            int rowIndex = Convert.ToInt32(row.RowIndex);

            CMTFeedback objFeedback = new CMTFeedback();
            DataTable dt = objFeedback.GetFeedbackDetails(ddlEvent.SelectedValue.ToString(), "0", rowIndex.ToString());
            excelExport(dt);
        }
    }

    private void excelExport(DataTable dt)
    {
        string fileName = "attachment;filename= Feedback" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xls";
        Response.Clear();
        Response.AddHeader("content-disposition", fileName);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        GridView gv = new GridView();
        gv.DataSource = dt;
        gv.DataBind();

        gv.RenderControl(htmlWrite);
        Response.Write(stringWrite);
        Response.Flush();
        Response.End();
    }

    private void ExporttoExcel(DataTable table)
    {

        if (table.Rows.Count > 0)
        {

            string fileName = "attachment;filename= UserProdReport_" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xls";
            Response.Clear();
            Response.AddHeader("content-disposition", fileName);
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/excel";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            GridView gv = new GridView();
            gv.DataSource = table;
            gv.DataBind();

            gv.RenderControl(htmlWrite);
            Response.Write(stringWrite);
            Response.Flush();

            Response.End();
        }

    }

    protected void dgFeedback_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        dgFeedback.PageIndex = e.NewPageIndex;
        bindFeedback();
        plFeedback.Visible = false;
    }

    protected void dgFeedbackSummary_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        dgFeedbackSummary.PageIndex = e.NewPageIndex;

        CMTFeedback objFeedback = new CMTFeedback();
        DataTable dt = objFeedback.GetFeedbackDetails(ddlEvent.SelectedValue.ToString(), ViewState["eventAttendeeId"].ToString(), "0");
        dgFeedbackSummary.DataSource = dt;
        dgFeedbackSummary.DataBind();
    }
    #endregion

    #region methods

    private void BindEvents()
    {
        CMTFeedback objFeedback = new CMTFeedback();
        DataTable dt = objFeedback.GetAllEventDetails(CurrentUser.GUI);
        ddlEvent.DataSource = dt;
        ddlEvent.DataTextField = "EventName";
        ddlEvent.DataValueField = "EventId";
        ddlEvent.DataBind();
        ddlEvent.Items.Insert(0, new ListItem("Select Event", "0"));
        ddlEvent.SelectedIndex = 0;

    }

    private void bindFeedback()
    {
        CMTFeedback objFeedback = new CMTFeedback();
        DataTable dt = objFeedback.GetFeedbackDetails(ddlEvent.SelectedValue.ToString(), "0", "0");
        if (dt.Rows.Count > 0)
        {
            dgFeedback.Visible = true;
            dgFeedbackSummary.Visible = false;
            dgFeedback.DataSource = dt;
            dgFeedback.DataBind();
        }
        else
        {
            dgFeedback.Visible = false;
            dgFeedbackSummary.Visible = false;
        }
    }
    #endregion
}

